package service

import (
	"mogu-go-v2/common"
	"mogu-go-v2/models"
	"regexp"
)

/**
 *
 * @author  镜湖老杨
 * @date  2021/2/4 1:40 下午
 * @version 1.0
 */

type pictureService struct{}

func (pictureService) GetTopOne() models.Picture {
	var picture models.Picture
	common.DB.Where("status=?", 1).Order("create_time desc").Last(&picture)
	return picture
}

func (pictureService) GetPicByContent(content string) ([] string, error) {
	regex := regexp.MustCompile(`<img[^>]+src="([^"]+)"`)
	matches := regex.FindAllStringSubmatch(content, -1)

	var urls []string
    for _, match := range matches {
        urls = append(urls, match[1])
    }
	if len(urls) == 0 {
		urls = []string{"https://www.shanhubei.com/wp-content/themes/shanhuke/static/img/thumbnail.png"}
	}
    return urls, nil

}

var PictureService = &pictureService{}
